package com.alex.goods.controller;

import com.alex.goods.entity.GoodsOrders;
import com.alex.goods.entity.InOutSum;
import com.alex.goods.entity.Result;
import com.alex.goods.service.OrdersService;
import com.alex.goods.service.impl.OrdersServiceImpl;
import com.alex.goods.util.Blank;
import com.alex.goods.vo.LayuiTableVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/orders")
public class OrdersController extends BasicServlet {

    private static final long serialVersionUID = 1L;
    private OrdersService service = new OrdersServiceImpl();

    public void addOrder(String goodsNo, String factory, String singlePrice, Integer num, String unit, String dateio,
                         String personName, String tel, String status) {
        service.addOrder(goodsNo, factory, singlePrice, num, unit, dateio, personName, tel, status);
    }

    public Object selectOrdersList(Integer page, Integer limit, String start, String end,
                                   String goodsNo, String goodsName, String standard1, String standard2, String standard3,
                                   String status1, String status2,
                                   String material1, String material2, String material3, String material4,
                                   HttpServletRequest request) {

        String sqlfrom = "  select o.id , g.goods_no , g.goods_name, g.standard, g.material,   "
                + "     o.order_no,o.factory, o.`dateio`, "
                + "    o.single_price ,o.num ,o.datetime,o.unit, o.person_name,o.tel , "
                + " o.`status` from goods g , orders o , goods_order go  where go.goods_no = g.goods_no and "
                + "	go.order_no = o.order_no  " ;

      //        sqlfrom +=   "  and isuse = 'on'   "  ;

        String sqlto = "   limit ?,?  ";

        if (!Blank.isBlank(goodsNo)) {
            //sqlfrom+= " and   ";
            sqlfrom = sqlfrom + "  and   g.goods_no = '" + goodsNo + "'   ";
            //flag = 1;
        }

        if (!Blank.isBlank(goodsName)) {
            sqlfrom = sqlfrom + "  and  instr(g.goods_name,  '" + goodsName + "'  ) >0 ";
        }

        if (!Blank.isBlank(material1) || !Blank.isBlank(material2) || !Blank.isBlank(material3)
                || !Blank.isBlank(material4)
        ) {

            sqlfrom += "  and  ";

            sqlfrom += "  ( ";

            int tmpflag = 0;

            if (!Blank.isBlank(material1)) {

                sqlfrom += "   g.material = '" + material1 + "'  ";
                tmpflag = 1;
            }

            if (!Blank.isBlank(material2)) {
                if (tmpflag == 1) {
                    sqlfrom += "   or  ";
                }
                sqlfrom += "   g.material = '" + material2 + "'  ";
                tmpflag = 1;
            }

            if (!Blank.isBlank(material3)) {

                if (tmpflag == 1) {
                    sqlfrom += "   or  ";
                }

                sqlfrom += "   g.material = '" + material3 + "'  ";
                tmpflag = 1;

            }

            if (!Blank.isBlank(material4)) {

                if (tmpflag == 1) sqlfrom += "  or ";

                sqlfrom += "   g.material = '" + material4 + "'  ";
                tmpflag = 1;

            }
            sqlfrom += "   )  ";
            //	flag = 1;
        }

        if (!Blank.isBlank(standard1) || !Blank.isBlank(standard2) || !Blank.isBlank(standard3)) {
            int tmpflag = 0;


            sqlfrom += "  and  ";

            sqlfrom += "  ( ";

            if (!Blank.isBlank(standard1)) {
                sqlfrom += "     g.standard =  '" + standard1 + "'   ";
                tmpflag = 1;
            }

            if (!Blank.isBlank(standard2)) {
                if (tmpflag == 1) {
                    sqlfrom += " or  ";
                }
                sqlfrom += "   g.standard =  '" + standard2 + "'   ";
                tmpflag = 1;
            }
            if (!Blank.isBlank(standard3)) {
                if (tmpflag == 1) sqlfrom += "  or ";
                sqlfrom += "    g.standard =  '" + standard3 + "'    ";
                tmpflag = 1;
            }
            sqlfrom += "   )  ";
            //	flag = 1;
        }

        if (!Blank.isBlank(status1) || !Blank.isBlank(status2)) {
            int tmpflag = 0;


            sqlfrom += "  and  ";

            sqlfrom += "  ( ";

            if (!Blank.isBlank(status1)) {
                sqlfrom += "     o.`status` =  '" + status1 + "'   ";
                tmpflag = 1;
            }

            if (!Blank.isBlank(status2)) {
                if (tmpflag == 1) {
                    sqlfrom += " or  ";
                }
                sqlfrom += "   o.`status` =  '" + status2 + "'   ";
                tmpflag = 1;
            }
            sqlfrom += "   )  ";
            //flag = 1;
        }





        if (!Blank.isBlank(start) || !Blank.isBlank(end)) {
            int tmpflag = 0;
            sqlfrom += "  and  ";
            sqlfrom += "  ( ";
            if (!Blank.isBlank(start)) {
                sqlfrom += "     date(o.`dateio`) >= date( '" + start + "' )  ";
                tmpflag = 1;
            }

            if (!Blank.isBlank(end)) {
                if (tmpflag == 1) {
                    sqlfrom += " and  ";
                }
                sqlfrom += "   date(o.`dateio`) <=  date( '" + end + "' )  ";
                tmpflag = 1;
            }
            sqlfrom += "   )  ";
            //flag = 1;
        }

        //String s = request.getp
        //System.out.println(goodsName);
        //	String []material = request.getParameterValues("material");
        //	System.out.println("********");
        //	System.out.println(material.toString());

        //  Map<String, String[]>	s = request.getParameterMap();

        //	System.out.println(s.get("status").length);


        String sql = "select count(1) from  (  " + sqlfrom + "   ) as z";


        System.out.println(sql);

        sqlfrom += sqlto;

        System.out.println(sqlfrom);


        List<GoodsOrders> list = service.selectOrdersList(sqlfrom, (page - 1) * limit, limit);

        // System.out.println(list.size());
        LayuiTableVo layuiTableVo = new LayuiTableVo();
        layuiTableVo.setCode(0);
        layuiTableVo.setCount(service.getOrdersNum(sql));

        layuiTableVo.setData(list);
        return layuiTableVo;

    }

    public Object selectSumList(
            Integer page, Integer limit, String start, String end,
            String goodsNo, String goodsName, String standard1, String standard2, String standard3,
            String status1, String status2,
            String material1, String material2, String material3, String material4,
            HttpServletRequest request
    ) {

        String sqlfrom = "	select g.goods_no, g.goods_name, g.standard,   "
                + "   g.material ,sum(o.num) sum , o.`status`   "
                + "   from orders o ,  goods g  ,   "
                + "   goods_order go  "
                + "  where o.order_no = go.order_no and g.goods_no = go.goods_no  ";


//        sqlfrom +=   "  and isuse = 'on'   " ;

        String sqlmid = "  GROUP BY  g.goods_no,  o.`status`   ";

        String sqlto = "   limit ?,?  ";


        if (!Blank.isBlank(goodsNo)) {
            //sqlfrom+= " and   ";
            sqlfrom = sqlfrom + "  and   g.goods_no = '" + goodsNo + "'   ";
            //flag = 1;
        }


        if (!Blank.isBlank(goodsName)) {
            sqlfrom = sqlfrom + "  and  instr(g.goods_name,  '" + goodsName + "'  ) >0 ";
        }


        if (!Blank.isBlank(material1) || !Blank.isBlank(material2) || !Blank.isBlank(material3)
                || !Blank.isBlank(material4)
        ) {
            sqlfrom += "  and  ";

            sqlfrom += "  ( ";

            int tmpflag = 0;

            if (!Blank.isBlank(material1)) {

                sqlfrom += "   g.material = '" + material1 + "'  ";
                tmpflag = 1;
            }

            if (!Blank.isBlank(material2)) {
                if (tmpflag == 1) {
                    sqlfrom += "   or  ";
                }
                sqlfrom += "   g.material = '" + material2 + "'  ";
                tmpflag = 1;
            }

            if (!Blank.isBlank(material3)) {

                if (tmpflag == 1) {
                    sqlfrom += "   or  ";
                }

                sqlfrom += "   g.material = '" + material3 + "'  ";
                tmpflag = 1;

            }

            if (!Blank.isBlank(material4)) {

                if (tmpflag == 1) sqlfrom += "  or ";

                sqlfrom += "   g.material = '" + material4 + "'  ";
                tmpflag = 1;

            }
            sqlfrom += "   )  ";
            //	flag = 1;
        }


        if (!Blank.isBlank(standard1) || !Blank.isBlank(standard2) || !Blank.isBlank(standard3)) {
            int tmpflag = 0;
            sqlfrom += "  and  ";

            sqlfrom += "  ( ";

            if (!Blank.isBlank(standard1)) {
                sqlfrom += "     g.standard =  '" + standard1 + "'   ";
                tmpflag = 1;
            }

            if (!Blank.isBlank(standard2)) {
                if (tmpflag == 1) {
                    sqlfrom += " or  ";
                }
                sqlfrom += "   g.standard =  '" + standard2 + "'   ";
                tmpflag = 1;
            }
            if (!Blank.isBlank(standard3)) {
                if (tmpflag == 1) sqlfrom += "  or ";
                sqlfrom += "    g.standard =  '" + standard3 + "'    ";
                tmpflag = 1;
            }
            sqlfrom += "   )  ";
            //	flag = 1;
        }

        if (!Blank.isBlank(status1) || !Blank.isBlank(status2)) {
            int tmpflag = 0;
            sqlfrom += "  and  ";
            sqlfrom += "  ( ";
            if (!Blank.isBlank(status1)) {
                sqlfrom += "     o.`status` =  '" + status1 + "'   ";
                tmpflag = 1;
            }
            if (!Blank.isBlank(status2)) {
                if (tmpflag == 1) {
                    sqlfrom += " or  ";
                }
                sqlfrom += "   o.`status` =  '" + status2 + "'   ";
                tmpflag = 1;
            }
            sqlfrom += "   )  ";
        }
        if (!Blank.isBlank(start) || !Blank.isBlank(end)) {
            int tmpflag = 0;
            sqlfrom += "  and  ";
            sqlfrom += "  ( ";
            if (!Blank.isBlank(start)) {
                sqlfrom += "     date(o.`dateio`) >= date( '" + start + "' )  ";
                tmpflag = 1;
            }
            if (!Blank.isBlank(end)) {
                if (tmpflag == 1) {
                    sqlfrom += " and  ";
                }
                sqlfrom += "   date(o.`dateio`) <=  date( '" + end + "' )  ";
                tmpflag = 1;
            }
            sqlfrom += "   )  ";
        }

        LayuiTableVo layuiTableVo = new LayuiTableVo();

        String sql = "  select count(1)  from (  " + sqlfrom + "  " + sqlmid + "  ) as z";

        System.out.println(sql);

        System.out.println(service.getInOutNum(sql));

        layuiTableVo.setCount(service.getInOutNum(sql));

        sqlfrom = sqlfrom + sqlmid + sqlto;

        System.out.println(sqlfrom);

        List<InOutSum> list = service.selectSumList(sqlfrom, (page - 1) * limit, limit);

        layuiTableVo.setCode(0);

        layuiTableVo.setData(list);
        return layuiTableVo;
    }


    public Result selectSumByGoodsNo(HttpServletRequest request,String goodsNo, String start, String end,String status1,String status2) {
        List<InOutSum> list = service.selectSumByGoodsNo(goodsNo,start,end,status1,status2);
        System.out.println(list);
        InOutSum ios = new InOutSum();
        ios.setSum(new BigDecimal("0"));
        if(list.size()==0){
            list.add(ios);
        }
        return Result.success(1, list);
    }
	/*
	      copyObject 
	{
	    "id": "11",
	    "goodsNo": "20230224000001",
	    "goodsName": "薇尔莉特*伊芙加登",
	    "factory": "江南皮革厂",
	    "standard": "小",
	    "material": "硅胶",
	    "singlePrice": "100",
	    "num": "50",
	    "unit": "顺丰",
	    "personName": "张三",
	    "tel": "8888866",
	    "statuss": " in "
	}
	*/

    public void updateOrder(
            String goodsNo,
            String orderNo,
            String factory,
            String singlePrice, Integer num, String dateio,
            String unit,
            String personName, String tel, String statuss
    ) {
        service.updateOrder(goodsNo, orderNo, factory, singlePrice, num, dateio, unit, personName, tel, statuss);
    }

    public void deleteOrderByOrderNo(String orderNo, String goodsNo) {
        System.out.println(orderNo + "   **************  " + goodsNo);
        service.deleteOrderByOrderNo(orderNo, goodsNo);
    }
}
